home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / X / cursor / fontCursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.3 KB  |  151 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*
  19.  * fontCursor.c
  20.  *
  21.  *   a simple example of changing a window's cursor using "Font" cursors.  
  22.  *   The cursors are refered to by an index which is defined in the include 
  23.  *   file <X11/cursorfont.h>.  Viewing cursor font definitions is possible
  24.  *   by using the utility `xfd' like so:  
  25.  *
  26.  *               xfd -fn cursor
  27.  */
  28.  
  29. #include <unistd.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <X11/Xlib.h>
  33. #include <X11/Xutil.h>
  34. #include <X11/cursorfont.h>
  35.  
  36. main(void)
  37. {
  38.   Display *dpy;
  39.   Window wind;
  40.   int scr;
  41.   unsigned int keep_going = 1;
  42.   XEvent ev;
  43.   unsigned int i;
  44.   XColor backg, ebackg, foreg, eforeg, ered, red;
  45.   Window rwin;
  46.   Colormap dcm;
  47.   GC dgc;
  48.   Cursor fontc[XC_num_glyphs];
  49.   unsigned char id[256];
  50.   static unsigned char drexions[] = "Mouse button to cycle thru Font Cursors.";
  51.   XWMHints    wmhints;
  52.   XSizeHints  wmsizehints;
  53.   XClassHint  classhint;
  54.   
  55.   dpy = XOpenDisplay("");
  56.   scr = DefaultScreen(dpy);
  57.   rwin = RootWindow(dpy, scr);
  58.   dcm = DefaultColormap(dpy, scr);
  59.   dgc = DefaultGC(dpy,scr);
  60.  
  61. #if defined(DEBUGIT)
  62.   XSynchronize(dpy,True);
  63. #endif
  64.   if( !XAllocNamedColor(dpy, dcm, "darkslategray", &backg, &ebackg)) {
  65.     fprintf( stderr, "Error getting DarkSlateGray\n" ); exit(1); }
  66.  
  67.   if( !XAllocNamedColor(dpy, dcm,"ivory", &foreg, &eforeg)) {
  68.     fprintf( stderr, "Error getting Ivory\n" ); exit(1); }
  69.  
  70.   if( !XAllocNamedColor(dpy, dcm,"red", &red, &ered)) {
  71.     fprintf( stderr, "Error getting Red\n" ); exit(1); }
  72.  
  73.   for( i = 0; i < XC_num_glyphs-1; i++ ) {
  74. #if defined(DEBUGIT)
  75.     printf("%d\n", i); fflush(stdout);
  76. #endif
  77.     fontc[i] = XCreateFontCursor(dpy, i);        
  78.     if( fontc[i] == NULL ) {
  79.       fprintf( stderr, "Error creating cursor from cursorfont %d\n", i );
  80.       exit(1);
  81.     }
  82.     XRecolorCursor( dpy, fontc[i], &foreg, &red );
  83.   }
  84.  
  85.   wind = XCreateSimpleWindow( dpy, rwin, 0, 0, 512, 512, 2,
  86.                               foreg.pixel, backg.pixel);
  87.  
  88.   classhint.res_name = "LEFT or RIGHT Mouse to Change Cursor";
  89.   classhint.res_class = "GlyphCursor";
  90.   XSetClassHint(dpy, wind, &classhint);
  91.  
  92.   wmhints.input = True;
  93.   wmhints.flags = InputHint;
  94.   XSetWMHints(dpy, wind, &wmhints);
  95.  
  96.   wmsizehints.x = 100;
  97.   wmsizehints.y = 100;
  98.   wmsizehints.width = 100;
  99.   wmsizehints.height = 100;
  100.   wmsizehints.flags = USPosition | USSize;
  101.   XSetWMNormalHints(dpy, wind, &wmsizehints);
  102.  
  103.   XSetWindowBackground( dpy, wind, backg.pixel );
  104.   XSetBackground( dpy, dgc, red.pixel );
  105.   XSetForeground( dpy, dgc, foreg.pixel );
  106.  
  107.   XSelectInput( dpy, wind, ButtonPressMask|KeyPressMask|ExposureMask);
  108.   XMapWindow( dpy, wind );
  109.   
  110.   i = 0;
  111.   do { 
  112.     XNextEvent( dpy, &ev );
  113.     switch( ev.type )
  114.     {
  115.       case Expose:
  116.       while( XCheckTypedEvent(dpy, Expose, &ev) );
  117.     XUndefineCursor( dpy, wind );
  118.     XClearWindow(dpy, wind);
  119.     XDefineCursor( dpy, wind, fontc[i%(XC_num_glyphs-1)] );
  120.     sprintf(id, "%d", i%(XC_num_glyphs-1));
  121.     XDrawString(dpy, wind, dgc, 200, 200, id, strlen(id));
  122.     XDrawString(dpy, wind, dgc, 20, 20, drexions, strlen(drexions));
  123.         break;
  124.  
  125.       case KeyPress:
  126.         XCloseDisplay(dpy);
  127.         keep_going = 0;
  128.         break;
  129.  
  130.       case ButtonPress:
  131.         if( ev.xbutton.button == Button1 )
  132.           i += 1;
  133.         else if( ev.xbutton.button == Button3 )
  134.           i -= 1;
  135.         else
  136.           i = 0;
  137.     XUndefineCursor( dpy, wind );
  138.     XClearWindow(dpy, wind);
  139.     XDefineCursor( dpy, wind, fontc[i%(XC_num_glyphs-1)] );
  140.     sprintf(id, "%d", i%(XC_num_glyphs-1));
  141.     XDrawString(dpy, wind, dgc, 200, 200, id, strlen(id));
  142.     XDrawString(dpy, wind, dgc, 20, 20, drexions, strlen(drexions));
  143.         break;
  144.  
  145.       default:
  146.         break;
  147.     }
  148.   } while( keep_going );
  149.   exit(0);
  150. }
  151.